package com.colin.aiyi.dao.ImpxUtils;

import com.colin.aiyi.app.Constant;
import com.colin.aiyi.app.MyApp;
import com.colin.aiyi.dao.AbsService;
import com.colin.aiyi.entity.BaseRequest;
import com.colin.aiyi.entity.response.BaseResponse;
import com.colin.aiyi.presenter.Interface.Callback;
import com.colin.aiyi.utils.NetUtils;
import com.google.gson.JsonSyntaxException;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.util.KeyValue;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by hl on 2017/6/9.
 */

public class AbsServicexUtils  extends AbsService implements org.xutils.common.Callback.CommonCallback<String>{

   public int msgType=-2;



    public  <E > void sendMsgToView(E e){
        EventBus.getDefault().post(e);
    }

    public  <E > void sendMsgToView(Class<E> e,String json){
        E baseResponse=gson.fromJson(json, e);
        if (baseResponse!=null)
        {
            EventBus.getDefault().post(baseResponse);
        }
    }
    public void sendToServer(RequestParams params){
        sendToServer(params,0);
    }
    public void sendToServer(RequestParams params,int post){
        sendToServer(params,post,false);
    }
    public void sendToServer(RequestParams params,int post,boolean isloading){
        if (NetUtils.isNetworkAvailable(MyApp.getInstance().getApplicationContext()) ){
        }else {
            BaseResponse baseResponse=new BaseResponse();
            baseResponse.setCode(8888);
            baseResponse.setMessage("网路错误，请链接网络！");
            sendMsgToView(baseResponse);
            return;
        }
        params.setConnectTimeout(5000);
        if (post==1)
        {

            x.http().post(params,this);
        }else {
            x.http().get(params,this);
        }
        if (isloading){
            sendMsgToView(new BaseRequest());
        }

        if (Constant.debug)
        {
            System.out.println("请求地址："+params.getUri());
            for (KeyValue keyValue : params.getQueryStringParams()) {
                System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            }
            for (KeyValue keyValue : params.getBodyParams()) {
                System.out.println("请求参数"+keyValue.key+":"+keyValue.getValueStr());
            }
        }

    }

    @Override
    public void onSuccess(String result) {
        if (Constant.debug){
            System.out.println("返回结果"+result);
        }
        try {
            BaseResponse baseResponse=gson.fromJson(result, BaseResponse.class);
            if (baseResponse.getCode()==4005){  //tocke过期发送到基类
                sendMsgToView(baseResponse);
            }
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }

    }




    @Override
    public void onError(Throwable ex, boolean isOnCallback) {
        BaseResponse baseResponse=new BaseResponse();
        baseResponse.setMsgType(Constant.CODE_NET_TIMEOUT_);
        baseResponse.setCode(8888);
        baseResponse.setMessage("链接超时，网络错误");
        sendMsgToView(baseResponse);
    }

    @Override
    public void onCancelled(CancelledException cex) {

    }

    @Override
    public void onFinished() {

    }

    @Override
    public <V> void getObject(V v) {

    }

    @Override
    public <V> void saveObject(V v) {

    }

    @Override
    public <V> void updateObject(V v) {

    }

    @Override
    public <V> void removeObject(V v) {

    }

    @Override
    public <T, V> void getObject(Callback<T> t, V v) {
        this.callback=t;
        getObject(v);
    }

    @Override
    public <T, V> void saveObject(Callback<T> t, V v) {
        this.callback=t;
        saveObject(v);
    }

    @Override
    public <T, V> void updateObject(Callback<T> t, V v) {
        this.callback=t;
       updateObject(v);
    }

    @Override
    public <T, V> void removeObject(Callback<T> t, V v) {
        this.callback=t;
       removeObject(v);
    }
}
